<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="费用标题" prop="title">
				<el-input v-model="form.title" placeholder="费用标题" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="费用总额" prop="totalCost">
						<el-input-number v-model="form.totalCost" clearable></el-input-number>/万元
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="支出时间" prop="happenTime">
						<el-date-picker v-model="form.happenTime" placeholder="支出时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>

				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="关联项目" prop="projectId">
						<el-cascader ref="projectCasCader" v-model="form.projectId" :options="projectIds" :props="projectProps" clearable
									 :show-all-levels="false" style="width: 100%;"></el-cascader>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="费用科目" prop="budgetAccount">
						<el-cascader v-model="form.budgetAccount" :options="budgetAccountList" :props="budgetAccountProps" clearable :show-all-levels="false" style="width: 100%;"></el-cascader>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="费用事由">
				<el-input v-model="form.remark" placeholder="费用事由" clearable maxlength="380" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'budget'}"  :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";
import AttachMenu from '@/views/module/attachMenu'
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserLevelDialog,
		selectUserDialog,
		selectDeptDialog,
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			titleMap: {
				add: '费用录入',
				edit: '费用编辑',
				show: '查看'
			},
			projectIds: [],
			projectProps: {
				label:"title",
				value: "projectId",
				emitPath: false,
				checkStrictly: true
			},
			budgetAccountList: [],
			budgetAccountProps: {
				label:"title",
				value: "budgetAccountId",
				emitPath: false,
				checkStrictly: true
			},
			fileList:[],
			apiObj:this.$API.file.attach.uploadFile,
			approvalUserList:[],
			form: {
				recordId:'',
				title: '',
				totalCost:0,
				happenTime: '',
				projectId:'',
				budgetAccount:'',
				remark:'',
				attachId:''
			},
			rules: {
				title: [
					{required: true, message: '申请标题不能为空', trigger: 'blur'}
				],
				happenTime:[
					{required: true, message: '支付时间不能为空', trigger: 'blur'}
				],
				budgetAccount:[
					{required: true, message: '费用科目不能为空', trigger: 'blur'}
				],
				projectId:[
					{required: true, message: '调整项目不能为空', trigger: 'blur'}
				],
				totalCost: [
					{required: true, message: '费用金额不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.getBudgetProjectList();
		this.getAllBudgetAccountList();
	},
	methods: {
		async getAllBudgetAccountList(){
			var res = await this.$API.finance.budget.getAllBudgetAccountList.get();
			this.budgetAccountList = res.data;
		},
		async getBudgetProjectList(){
			var res = await this.$API.finance.budget.getAllBudgetProjectList.get();
			this.projectIds = res.data;
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.finance.budget.insertBudgetCost.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.finance.budget.updateBudgetCost.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {

			this.form.recordId = data.recordId
			this.form.title = data.title
			this.form.totalCost = data.cTotalCost
			this.form.projectId = data.projectId
			this.form.happenTime = data.happenTime
			this.form.budgetAccount = data.budgetAccount
			this.form.remark = data.remark
			this.form.attachId = data.attachId
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
